package com.zs.main.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.zs.main.filter.ApiLogFilter;
import com.zs.main.filter.GlobalFilter;
import com.zs.main.filter.RequestReplaceFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;


@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {


    /**
    * 添加Long转json精度丢失的配置
    * @Return: void
    */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(jackson2HttpMessageConverter);
    }

    /**
     * 替换HttpServletRequest，使body中内容可重复读取
     */
    @Bean
    public FilterRegistrationBean registerRequestReplaceFilter(RequestReplaceFilter requestReplaceFilter) {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(requestReplaceFilter);
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("requestReplaceFilter");
        registrationBean.setOrder(-105);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean registerLoginCheckFilter(GlobalFilter globalFilter) {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(globalFilter);
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("globalFilter");
        registrationBean.setOrder(-104);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean registerMonitorFilter(ApiLogFilter apiLogFilter) {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(apiLogFilter);
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("monitorFilter");
        registrationBean.setOrder(-103);
        return registrationBean;
    }

    @Bean
    public RequestContextListener requestContextListener(){
        return new RequestContextListener();
    }
}
